﻿using MediatR;
using SmartCity.ProcessEngine.API.Domain;
using SmartCity.ProcessEngine.API.ErrorCode;
using SmartCity.ProcessEngine.API.Infrastructrue.Interfaces;
using SmartCity.ProcessEngine.API.Read.Applications.Commands;
using SmartCity.ProcessEngine.API.Read.DTOS;

namespace SmartCity.ProcessEngine.API.Read.Applications.CommandHandlers
{
    /// <summary>
    /// 表单查询命令
    /// </summary>
    public class FormTypeCascaderQueryCommandHandler : IRequestHandler<FormTypeCascaderQueryCommand, APIResult<List<FormTypeDTO>>>
    {
        /// <summary>
        /// 表单仓储
        /// </summary>
        private readonly IBaseRespository<WF_FromType> TypeRep;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="typeRep">分类仓储</param>
        public FormTypeCascaderQueryCommandHandler(IBaseRespository<WF_FromType> typeRep)
        {
            TypeRep = typeRep;
        }

        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<List<FormTypeDTO>>> Handle(FormTypeCascaderQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<FormTypeDTO>> result=new APIResult<List<FormTypeDTO>>();
            result.Code=APIResultCode.SearchSuccess;
            result.Message=APIResultCode.SearchSuccess.ToString();

            var res = from a in TypeRep.GetAll()
                      where a.IndustryId == request.IndustryId
                      select new FormTypeDTO
                      {
                          TypeName=a.TypeName,
                          TypeNo=a.TypeNo,
                      };
            result.Data = res.ToList();
           return Task.FromResult(result);
        }
    }
}
